package tt.srvinst1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;

@RestController
public class ConsumerController {
    private static final String SERVICE_NAME = "srvinst2";

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 获取所有服务
     */
    @RequestMapping("/services")
    public Object services() {
        return discoveryClient.getInstances(SERVICE_NAME);
    }

    @Autowired
    private ProducerService producerService;
    @Autowired
    private Tt8Service tt8Service;

    @RequestMapping("/callSayHello")
    public String services(@RequestParam("name") String name) {
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances(SERVICE_NAME);
//        ServiceInstance serviceInstance = (ServiceInstance) discoveryClient.getInstances(SERVICE_NAME);
        ServiceInstance serviceInstance = serviceInstanceList.get(0);
//        String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
//        System.out.println(callServiceResult);
//        String callResult = restTemplate.getForObject("http://srvinst2/hello", String.class);

//        ServiceInstance serviceInstance = serviceInstanceList.get(0);
        MultiValueMap<String, String> pMap = new LinkedMultiValueMap<>();
        pMap.add("name", "test99999");
        return restTemplate.postForObject("http://srvinst2/hello", pMap, String.class);
//        return restTemplate.getForEntity("http://srvinst2/hello?name=1234", String.class).getBody();
//        return producerService.hello("acc");
//        return tt8Service.testHello();
    }

}
